package com.whfp.coky.ui.entry.vm

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.whfp.coky.aop.CheckNet
import com.whfp.coky.api.response.LoginStaffBean
import com.whfp.coky.api.retrofit.RetrofitClient
import com.whfp.coky.app.AppDatas
import com.whfp.coky.databinding.ActivityLoginBinding
import com.whfp.coky.other.KeyboardWatcher
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.base.BaseViewModel
import com.whfp.coky.ui.main.TaskMainAct
import com.whfp.coky.utils.LogUtil

/**
 *
 *
 * created by coky on 2022/2/22
 */
class LoginVM: BaseViewModel<ActivityLoginBinding>(),
    KeyboardWatcher.SoftKeyboardStateListener{

    var loginLD = MutableLiveData<LoginStaffBean>()//登录返回

    /** logo 缩放比例 */
    private val logoScale: Float = 0.8f

    /** 动画时间 */
    private val animTime: Int = 300

    @CheckNet
    fun commit(){
        LogUtil.e("commit....")
        launch({httpUtil.loginStaff(
            vBinding.etLoginPhone.text.toString(),
            vBinding.etLoginPassword.text.toString(),
            getDeviceBarCode(),
            0,1)}
            ,loginLD,false,true)
    }


    //    获取设备mac
    private fun getDeviceBarCode(): String {
        return "20220221103907535"
    }

    override fun observe(activity: BaseActivity<*,*>, owner: LifecycleOwner) {
        loginLD.observe(owner, Observer {
            vBinding.btnLoginCommit.showSucceed()
            AppDatas.getInstance().staffBean = loginLD.value
            RetrofitClient.getInstance().addTokenHeader(loginLD.value!!.access_token!!)
            activity.postDelayed({
                activity.startActivity(TaskMainAct::class.java)
                activity.finish()
                                 },400)
        })
    }

    override fun onSoftKeyboardOpened(keyboardHeight: Int) {
        // 执行位移动画
        vBinding.llLoginBody?.let {
            val objectAnimator: ObjectAnimator = ObjectAnimator.ofFloat(it,
                "translationY", 0f, (-(vBinding.btnLoginCommit?.height?.toFloat() ?: 0f)))
            objectAnimator.duration = animTime.toLong()
            objectAnimator.interpolator = AccelerateDecelerateInterpolator()
            objectAnimator.start()
        }

        // 执行缩小动画
        vBinding.includeHead.llLogoHead.let {
            it.pivotX = it.width / 2f
            it.pivotY = it.height.toFloat()
            val animatorSet = AnimatorSet()
            val scaleX = ObjectAnimator.ofFloat(it, "scaleX", 1f, logoScale)
            val scaleY = ObjectAnimator.ofFloat(it, "scaleY", 1f, logoScale)
            val translationY = ObjectAnimator.ofFloat(it, "translationY",
                0f, (-(vBinding.btnLoginCommit?.height?.toFloat() ?: 0f)))
            animatorSet.play(translationY).with(scaleX).with(scaleY)
            animatorSet.duration = animTime.toLong()
            animatorSet.start()
        }

    }

    override fun onSoftKeyboardClosed() {
        // 执行位移动画
        vBinding.llLoginBody?.let {
            val objectAnimator: ObjectAnimator = ObjectAnimator.ofFloat(it,
                "translationY", it.translationY, 0f)
            objectAnimator.duration = animTime.toLong()
            objectAnimator.interpolator = AccelerateDecelerateInterpolator()
            objectAnimator.start()
        }

        // 执行放大动画
        vBinding.includeHead.llLogoHead?.let {
            it.pivotX = it.width / 2f
            it.pivotY = it.height.toFloat()

            if (it.translationY == 0f) {
                return
            }

            val animatorSet = AnimatorSet()
            val scaleX: ObjectAnimator = ObjectAnimator.ofFloat(it, "scaleX", logoScale, 1f)
            val scaleY: ObjectAnimator = ObjectAnimator.ofFloat(it, "scaleY", logoScale, 1f)
            val translationY: ObjectAnimator = ObjectAnimator.ofFloat(it,
                "translationY", it.translationY, 0f)
            animatorSet.play(translationY).with(scaleX).with(scaleY)
            animatorSet.duration = animTime.toLong()
            animatorSet.start()
        }

    }
}